function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')
  return `${t1} ${t2}`
}

export function formatDate(time) {
  if (!time) {
    return false
  }
  let date = new Date(time);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  month = month < 10 ? "0" + month : month;
  day = day < 10 ? "0" + day : day;
  return year + '-' + month + '-' + day
}

export function lostLogin() {
  mpvue.showModal({
    title: "提示",
    content: "请先登录",
    cancel: false,
    complete: function (res) {
      if (res.confirm) {
        mpvue.navigateTo({
          url: "../login/main"
        });
      }
    }
  });
}

export default {
  formatNumber,
  formatTime,
  lostLogin,
  formatDate
}
